import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:osc/res/dimens.dart';
import 'package:osc/ui/widgets/image_loader.dart';
import 'package:osc/ui/pages/user/user_profile.dart';
import 'package:osc/utils/navigator_util.dart';
import 'package:osc/utils/pd_util.dart';

class OscPortraitWidget extends StatelessWidget {
  final int id;
  final String portraitImageUrl;
  final double size;
  final bool showGender;
  final int gender;

  const OscPortraitWidget({Key key, this.id, this.portraitImageUrl, this.size = Dimens.view_portrait_size, this.showGender = false, this.gender = 0}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        NavigatorUtil.pushPage(context, UserProfile(userId: id));
      },
      child: Container(
        width: size,
        height: size,
        child: showGender? buildPortraitWithGender() : buildPortrait(),
      ),
    );
  }

  buildPortraitWithGender(){
    return Stack(
      children: <Widget>[
        buildWidgetWithWhiteCircle(
            child: buildPortrait(),
            size: size,
            padding: Dimens.gap_view_smaller,
        ),
        Positioned(
          right: 0,
            bottom: 0,
            child: Offstage(
              offstage: gender != 1 && gender != 2,
              child: buildWidgetWithWhiteCircle(
                child: buildGender(gender),
                size: Dimens.view_item_icon_height_smaller,
                padding: 0.3,
              ),
            )
        ),
      ],
    );
  }

  buildWidgetWithWhiteCircle({@required Widget child, @required double size, @required double padding}){
    return Container(
      width: size,
      height: size,
      padding: EdgeInsets.all(padding),
      decoration: BoxDecoration(
        color: Colors.white,
        shape: BoxShape.circle,
      ),
      child: child,
    );
  }

  buildPortrait(){
    return ClipOval(
      child: ImageLoader(url: portraitImageUrl),
    );
  }

  buildGender(int gender){
    String imageName = gender == 1? "ic_male" : "ic_female";
    return Image.asset(PdUtil.getImagePath(imageName),);
  }
}
